<?php

  session_start();
  include "config.php";
  include "ALcustomer.php";

  unset($_SESSION["cs_surname"]);
  unset($_SESSION["cs_given_name"]);
  unset($_SESSION["cs_dob_day"]);
  unset($_SESSION["cs_dob_month"]);
  unset($_SESSION["cs_dob_year"]);
  unset($_SESSION["cs_address"]);
  unset($_SESSION["cs_pcode"]);
  unset($_SESSION["cs_ph_area1"]);
  unset($_SESSION["cs_ph_no1"]);

  unset($_SESSION["cr_title"]);
  unset($_SESSION["cr_surname"]);
  unset($_SESSION["cr_given_name"]);
  unset($_SESSION["cr_dob_day"]);
  unset($_SESSION["cr_dob_month"]);
  unset($_SESSION["cr_dob_year"]);
  unset($_SESSION["cr_address"]);
  unset($_SESSION["cr_pcode"]);
  unset($_SESSION["cr_ph_area1"]);
  unset($_SESSION["cr_ph_no1"]);
  unset($_SESSION["cr_ph_area2"]);
  unset($_SESSION["cr_ph_no2"]);
  unset($_SESSION["cr_card_type1"]);
  unset($_SESSION["cr_card_name1"]);
  unset($_SESSION["cr_card_no1_1"]);
  unset($_SESSION["cr_card_no1_2"]);
  unset($_SESSION["cr_card_no1_3"]);
  unset($_SESSION["cr_card_no1_4"]);
  unset($_SESSION["cr_card_expiry_month1"]);
  unset($_SESSION["cr_card_expiry_year1"]);
  unset($_SESSION["cr_card_type2"]);
  unset($_SESSION["cr_card_name2"]);
  unset($_SESSION["cr_card_no2_1"]);
  unset($_SESSION["cr_card_no2_2"]);
  unset($_SESSION["cr_card_no2_3"]);
  unset($_SESSION["cr_card_no2_4"]);
  unset($_SESSION["cr_card_expiry_month2"]);
  unset($_SESSION["cr_card_expiry_year2"]);
  
  unset($_SESSION["customerID"]);

  if(isset($_SESSION["user"]))
  {
    include "$header";
	include "dobPicker.js";
    include "$body";
?>

<div style="position:absolute; top:0px; left:1px; border:0;">
  <span class="page">Record New Customer Details:Search</span>
</div>

<?php

    include "$H_rncd";

    if(isset($_POST["search"]))
    {
      $surname = ltrim($_POST["surname"]);
      $given_name = ltrim($_POST["given_name"]);
      $dob_day = ltrim($_POST["days"]);
      $dob_month = ltrim($_POST["months"]);
      $dob_year = ltrim($_POST["years"]);
      $address = ltrim($_POST["address"]);
      $pcode = ltrim($_POST["pcode"]);
      $ph_area = ltrim($_POST["ph_area"]);
      $ph_no = ltrim($_POST["ph_no"]);

      //checks if entered data is valid and if the customer
      if(validSearchDetails($surname, $given_name, $dob_day, $dob_month, $dob_year, $address, $pcode, $ph_area, $ph_no, "new") == true)
      {
        $_SESSION["cs_surname"] = $surname;
        $_SESSION["cs_given_name"] = $given_name;
        $_SESSION["cs_dob_day"] = $dob_day;
        $_SESSION["cs_dob_month"] = $dob_month;
        $_SESSION["cs_dob_year"] = $dob_year;
        $_SESSION["cs_address"] = $address;
        $_SESSION["cs_pcode"] = $pcode;
        $_SESSION["cs_ph_area1"] = $ph_area;
        $_SESSION["cs_ph_no1"] = $ph_no;

        echo "<SCRIPT language='Javascript'>";
        echo "  window.location.href = 'recordNewCustomerDetails_1recordDetails.php';";
        echo "</SCRIPT>";
      }
      else
      {
        showForm();
        echo "<SCRIPT language='Javascript'>";
        echo "  customerForm.surname.value='".$surname."';";
        echo "  customerForm.given_name.value='".$given_name."';";
        echo "  customerForm.days.selectedIndex=".($dob_day - 1).";";
        echo "  customerForm.months.selectedIndex=".($dob_month - 1).";";
        echo "  customerForm.years.selectedIndex=".($dob_year - 1900).";";
        echo "  customerForm.address.value='".$address."';";
        echo "  customerForm.pcode.value='".$pcode."';";
        echo "  customerForm.ph_area.value='".$ph_area."';";
        echo "  customerForm.ph_no.value='".$ph_no."';";
        echo "</SCRIPT>";
      }
    }
    else if(isset($_SESSION["c_surname"]))
    {
      showForm();
      echo "<SCRIPT language='Javascript'>";
      echo "  customerForm.surname.value='" . $_SESSION["c_surname"] . "';";
      echo "  customerForm.given_name.value='" . $_SESSION["c_given_name"] . "';";
      echo "  customerForm.days.selectedIndex=" . ($_SESSION["c_day"]-1) . ";";
      echo "  customerForm.months.selectedIndex=" . ($_SESSION["c_month"]-1) . ";";
      echo "  customerForm.years.value=" . $_SESSION["c_year"] . ";";
      echo "  customerForm.address.value='" . $_SESSION["c_address"] . "';";
      echo "  customerForm.pcode.value='" . $_SESSION["c_pcode"] ."';";
      echo "  customerForm.ph_area.value='" . $_SESSION["c_ph_area"] . "';";
      echo "  customerForm.ph_no.value='" . $_SESSION["c_ph_no"] . "';";
      echo "</SCRIPT>";
    }
    else
    {
      showForm();
    }
    include "$footer";
  }
  else
  {
    session_destroy();
    echo "<SCRIPT language='Javascript'>";
    echo "  parent.window.location.href = 'index.php';";
    echo "</SCRIPT>";
  }

  function showForm()
  {
?>


<b>SEARCH: CHECK FOR EXISTING CUSTOMER</b>
<p>
<font color="red">*</font><font face="Century Gothic" style="font-size:12px"> = mandatory field</font>
<p>
<FORM name="customerForm" action="" method="post">
<TABLE class="customer" cellpadding="5" cellspacing="5">
  <TR>
    <TD class="cField">
      Surname<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      <input type="text" class="customer" name="surname" style="width:200px;" />
    </TD>
  </TR>
  <TR>
    <TD class="cField">
      Given Name<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      <input type="text" class="customer" name="given_name" style="width:200px;" />
    </TD>
  </TR>
  <TR>
    <TD class="cField">
      Date of Birth<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      <SCRIPT>fill_select(document.customerForm);year_install(document.customerForm)</script>
    </TD>
  </TR>
  <TR>
    <TD class="cField">
      Address<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      <input type="text" class="customer" name="address" style="width:200px;" />
    </TD>
  </TR>
  <TR>
    <TD class="cField">
      Post Code<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      <input type="text" class="customer" name="pcode" style="width:50px;" />
    </TD>
  </TR>
  <TR>
    <TD class="cField">
      Phone (H)/(M)<font color="red">*</font>
    </TD>
    <TD class="cDetails">
      ( <input type="text" class="customer" name="ph_area" style="width:30px;" /> ) <input type="text" class="customer" name="ph_no" style="width:100px;" />
    </TD>
  </TR>
</TABLE>
<p>
<p>
<input class="submit_off" type="reset" value="Reset" name="reset" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />
&nbsp;&nbsp;
<input class="submit_off" type="submit" value="Search" name="search" onmouseover="className='submit_on'" onmouseout="className='submit_off'" />
</FORM>
</CENTER>
</BODY>

<?php

  }

?>